using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class fireSkill : abstuctSkill
{
    public float fireDuration;
    private float delay = 0;

    protected override void onExecute()
    {
        if (source.tag != "Player") { return; }
        this.initialTransform = source.transform;
        this.initialPosition = new Vector3(source.transform.position.x, source.transform.position.y, -1);
        this.gameObject.transform.position = initialPosition;

        gameObject.SetActive(true);
    }

    // Start is called before the first frame update
    void Start()
    {
        //init();
    }

    // Update is called once per frame
    void Update()
    {
        if (source.tag != "Player") { return; }

        this.gameObject.transform.position=source.transform.position;

        if (delay < fireDuration / 1000.0f)
        {


            delay += Time.deltaTime;
        }
        else
        {
            Destroy(this.gameObject);
            source.GetComponent<Animator>().Play(Animator.StringToHash("idle"));
        }
        //该技能可以朝一个方向滑动，但是中途不能改变方向
        if (Input.GetKeyDown(source.GetComponent<playerControl>().left) 
            || Input.GetKeyDown(source.GetComponent<playerControl>().right)
            || Input.GetKeyDown(source.GetComponent<playerControl>().up)
            || Input.GetKeyDown(source.GetComponent<playerControl>().down))
        {
            source.GetComponent<Animator>().Play(Animator.StringToHash("idle"));

            Destroy(this.gameObject);
        }
    }
    /// <summary>
    /// 当技能碰到物体时调用此函数
    /// </summary>
    /// <param name="collision"></param>
    public void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log(" gameObject:" + collision.gameObject.name);
        //如果物体为玩家并且不是技能释放者
        if (source != collision.gameObject && collision.gameObject.tag == "Player")
        {

            collision.gameObject.GetComponent<playerControl>().hit(this.source, basicDamage);
            addBuffsToPlayer(collision.gameObject);
        }


    }
}
